/*
    <one line to give the program's name and a brief idea of what it does.>
    Copyright (C) <year>  <name of author>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "MapManager.h"
#include "File.h"
#include "BaseTypes.h"

#include <string>
#include <sstream>


using namespace std;

static SingletonDestroyer<MapManager> destroyer;



MapManager::MapManager()
:_map(NULL)
{
   
}


MapManager::~MapManager()
{
   if(_map)
   {
      delete _map;
      _map = NULL;
   }
}


void MapManager::LoadMap(const std::string & iMapName)
{
   File mapFile(iMapName);

   //#warning ---> Gerer les cas d'erreurs <---

   Map * map = new Map;

   string line = mapFile.readLine();

   istringstream is(line);

   int nbLine = 0, nbCol = 0;

   is >> nbLine;
   is >> nbCol;
   
   map->Init(nbLine, nbCol);

   string mapStr = mapFile.readAllSincePositionReverse();

   is.clear();

   is.str(mapStr);
   
   const unsigned int nbME = nbLine * nbCol;
   
   for(unsigned int i = 0 ; i < nbME ; ++i)
   {
      MEID meID;
      
      is >> meID;

      map->SetME(i, _rules.GetME(meID));
   }

   //Maybe we need to handle a tab of map !
   if(_map != NULL)
      delete _map;
   
   _map = map;
}




